package model;

import java.util.Date;
import java.util.ArrayList;

/**
 *
 * @author Martin Mohr Pedersen
 */
public class Order {

    private ArrayList<OrderLineItem> lineItems;
    private int orderNo;
    private Customer customer;
    private Delivery delivery;
    private double discount;
    private Date date;
    private Date paymentDate;
    private Date deliveryDate;
    private String paymentType;
    private double deliveryFee; 
    private double cashOnDeliveryFee;
    private double total;

    public Order() {
        lineItems = new ArrayList<OrderLineItem>();
        date = new Date();
    }

    public void addItem(Item item, int qty) {

        if (containsItem(item)) { // check if item is already added
            OrderLineItem line = getLineForItem(item);
            qty += line.getQty(); // add to new qty
            line.setQty(qty);
        } else {
            OrderLineItem line = new OrderLineItem(item, qty);
            lineItems.add(line);
        }
    }

    /**
     * Returns the SaleLineItem at given index
     * @param index
     * @return SaleLineItem at given index
     */
    public OrderLineItem getLine(int index) {
        return lineItems.get(index);
    }

    public void removeLine(OrderLineItem line) {
        lineItems.remove(line);
    }

    /**
     * Checks if sale contains specified item
     *
     * @param item
     *            Item to check for
     * @return True if sale contains item, false otherwise
     */
    public boolean containsItem(Item item) {
        OrderLineItem line = getLineForItem(item);
        return line != null;
    }

    /**
     * Fetches SaleLineItem object for specified item
     *
     * @param item
     *            Item to search for
     * @return SaleLineItem containing item, null if no such item
     */
    public OrderLineItem getLineForItem(Item item) {
        for (OrderLineItem line : lineItems) {
            if (line.getItem().equals(item)) {
                return line;
            }
        }

        return null;
    }

    /**
     * Calculates total value of the sale
     *
     * @return total value of sale
     */
    public double getTotal() {

        double TotalValue = 0;

        TotalValue += getSubTotal();

        double saving = getSavings();

        TotalValue -= saving;

        if (getDelivery() != null) {
            TotalValue += getDelivery().getPrice();
        }

        return TotalValue;

    }

    public double getSubTotal() {
        double total = 0;

        for (OrderLineItem lineItem : lineItems) {
            total += (lineItem.getItem().getTotalPrice() * lineItem.getQty());
        }

        return total;
    }

    public void close() {
        
    }

    public OrderLineItem[] getLineItems() {
        return lineItems.toArray(new OrderLineItem[lineItems.size()]);
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setDelivery(Delivery delivery) {
        this.delivery = delivery;
    }

    public Delivery getDelivery() {
        return delivery;
    }

    public void setDiscount(double discount) {
        this.discount = discount;
    }

    public double getSavings() {
        return (getDiscount() / 100) * getSubTotal();
    }

    public void clearCustomer() {
        this.customer = null;
    }

    public void clearDelivery() {
        this.delivery = null;
    }

    /**
     * @return the orderNo
     */
    public int getOrderNo() {
        return orderNo;
    }

    /**
     * @param orderNo the orderNo to set
     */
    public void setOrderNo(int orderNo) {
        this.orderNo = orderNo;
    }

    /**
     * @return the getDate
     */
    public Date getDate() {
        return date;
    }

    /**
     * @param getDate the getDate to set
     */
    public void setDate(Date getDate) {
        this.date = getDate;
    }

    /**
     * @return the discount
     */
    public double getDiscount() {
        return discount;
    }

    /**
     * @return the paymentDate
     */
    public Date getPaymentDate() {
        return paymentDate;
    }

    /**
     * @param paymentDate the paymentDate to set
     */
    public void setPaymentDate(Date paymentDate) {
        this.paymentDate = paymentDate;
    }

    /**
     * @return the deliveryDate
     */
    public Date getDeliveryDate() {
        return deliveryDate;
    }

    /**
     * @param deliveryDate the deliveryDate to set
     */
    public void setDeliveryDate(Date deliveryDate) {
        this.deliveryDate = deliveryDate;
    }

    /**
     * @return the paymentType
     */
    public String getPaymentType() {
        return paymentType;
    }

    /**
     * @param paymentType the paymentType to set
     */
    public void setPaymentType(String paymentType) {
        this.paymentType = paymentType;
    }

    /**
     * @return the deliveryFee
     */
    public double getDeliveryFee() {
        return deliveryFee;
    }

    /**
     * @param deliveryFee the deliveryFee to set
     */
    public void setDeliveryFee(double deliveryFee) {
        this.deliveryFee = deliveryFee;
    }

    /**
     * @return the cashOnDeliveryFee
     */
    public double getCashOnDeliveryFee() {
        return cashOnDeliveryFee;
    }

    /**
     * @param cashOnDeliveryFee the cashOnDeliveryFee to set
     */
    public void setCashOnDeliveryFee(double cashOnDeliveryFee) {
        this.cashOnDeliveryFee = cashOnDeliveryFee;
    }

    /**
     * @param lineItems the lineItems to set
     */
    public void setLineItems(ArrayList<OrderLineItem> lineItems) {
        this.lineItems = lineItems;
    }

    /**
     * @param total the total to set
     */
    public void setTotal(double total) {
        this.total = total;
    }
    
}
